package com.example.studentportrait.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.studentportrait.domain.ResponseResult;
import com.example.studentportrait.domain.dto.GraduationDestinationDto;
import com.example.studentportrait.domain.entity.GraduationDestination;
import com.example.studentportrait.domain.entity.TeacherStudent;
import com.example.studentportrait.domain.entity.User;
import com.example.studentportrait.domain.entity.Student;
import com.example.studentportrait.domain.vo.GraduationDestinationVo;
import com.example.studentportrait.domain.vo.PageVo;
import com.example.studentportrait.mapper.GraduationDestinationMapper;
import com.example.studentportrait.mapper.StudentMapper;
import com.example.studentportrait.mapper.TeacherStudentMapper;
import com.example.studentportrait.mapper.UserMapper;
import com.example.studentportrait.service.GraduationDestinationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;


@Service("GraduationDestinationService")
public class GraduationDestinationImpl extends ServiceImpl<GraduationDestinationMapper, GraduationDestination> implements GraduationDestinationService {

    @Autowired
    private TeacherStudentMapper teacherStudentMapper;

    @Autowired
    private GraduationDestinationMapper graduationDestinationMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private StudentMapper studentMapper;

//    @Override
//    public ResponseResult graduationDestinationList(int page, int size, GraduationDestinationDto graduationDestinationDto) {
//        // 获取当前登录教师账号
//        String teacherAccount = "100513gzu";
//
//        // 查询教师所管理的学生账号
//        LambdaQueryWrapper<TeacherStudent> tsWrapper = new LambdaQueryWrapper<>();
//        tsWrapper.eq(TeacherStudent::getTeacherAccount, teacherAccount);
//        List<String> studentAccounts = teacherStudentMapper.selectList(tsWrapper)
//                .stream()
//                .map(TeacherStudent::getStudentAccount)
//                .collect(Collectors.toList());
//
//        // 如果没有管理的学生，直接返回空分页
//        if (studentAccounts.isEmpty()) {
//            PageVo emptyPage = new PageVo(Collections.emptyList(), 0L);
//            return ResponseResult.okResult(emptyPage);
//        }
//
//        // 创建分页对象
//        Page<GraduationDestination> pagination = new Page<>(page, size);
//
//        // 构建查询条件
//        LambdaQueryWrapper<GraduationDestination> queryWrapper = new LambdaQueryWrapper<>();
//
//        if (StringUtils.hasText(graduationDestinationDto.getAccount())) {
//            queryWrapper.eq(GraduationDestination::getAccount, graduationDestinationDto.getAccount());
//        }
//
//        if (StringUtils.hasText(graduationDestinationDto.getUserName())) {
//            queryWrapper.inSql(GraduationDestination::getAccount,
//                    "SELECT account FROM user WHERE user_name LIKE '%" + graduationDestinationDto.getUserName() + "%'");
//        }
//
//        if (StringUtils.hasText(graduationDestinationDto.getMajor())) {
//            queryWrapper.inSql(GraduationDestination::getAccount,
//                    "SELECT account FROM student WHERE major LIKE '%" + graduationDestinationDto.getMajor() + "%'");
//        }
//
//        // 分页查询
//        IPage<GraduationDestination> resultPage = graduationDestinationMapper.selectPage(pagination, queryWrapper);
//
//        // 获取查询结果
//        List<GraduationDestination> graduationDestinationList = resultPage.getRecords();
//
//        // 转换为 GraduationDestinationVo 对象
//        List<GraduationDestinationVo> voList = graduationDestinationList.stream().map(destination -> {
//            GraduationDestinationVo vo = new GraduationDestinationVo();
//            // 查询用户信息
//            User user = userMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getAccount, destination.getAccount()));
//            // 查询学生信息
//            Student student = studentMapper.selectOne(new LambdaQueryWrapper<Student>().eq(Student::getAccount, destination.getAccount()));
//            vo.setAccount(destination.getAccount());
//            vo.setUserName(user.getUserName());
//            vo.setMajor(student.getMajor());
//            vo.setCollege(student.getCollege());
//            vo.setCompanyName(destination.getCompanyName());
//            vo.setStation(destination.getStation());
//            vo.setAddress(destination.getAddress());
//            vo.setWage(destination.getWage());
//            return vo;
//        }).collect(Collectors.toList());
//
//        // 返回分页结果
//        PageVo pageVo = new PageVo(voList, resultPage.getTotal());
//        return ResponseResult.okResult(pageVo);
//    }

    @Override
    public ResponseResult graduationDestinationList(int page, int size, GraduationDestinationDto graduationDestinationDto) {
        // 获取当前登录教师账号
        String teacherAccount = "100513gzu";

        // 查询教师所管理的学生账号
        LambdaQueryWrapper<TeacherStudent> tsWrapper = new LambdaQueryWrapper<>();
        tsWrapper.eq(TeacherStudent::getTeacherAccount, teacherAccount);
        List<String> studentAccounts = teacherStudentMapper.selectList(tsWrapper)
                .stream()
                .map(TeacherStudent::getStudentAccount)
                .collect(Collectors.toList());

        // 如果没有管理的学生，直接返回空分页
        if (studentAccounts.isEmpty()) {
            PageVo emptyPage = new PageVo(Collections.emptyList(), 0L);
            return ResponseResult.okResult(emptyPage);
        }

        // 创建分页对象
        Page<GraduationDestination> pagination = new Page<>(page, size);

        // 构建查询条件
        LambdaQueryWrapper<GraduationDestination> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(GraduationDestination::getAccount, studentAccounts);

        if (StringUtils.hasText(graduationDestinationDto.getAccount())) {
            queryWrapper.eq(GraduationDestination::getAccount, graduationDestinationDto.getAccount());
        }

        if (StringUtils.hasText(graduationDestinationDto.getUserName())) {
            queryWrapper.apply("account IN (SELECT account FROM user WHERE user_name LIKE CONCAT('%', {0}, '%'))", graduationDestinationDto.getUserName());
        }

        if (StringUtils.hasText(graduationDestinationDto.getMajor())) {
            queryWrapper.apply("account IN (SELECT account FROM student WHERE major LIKE CONCAT('%', {0}, '%'))", graduationDestinationDto.getMajor());
        }

        // 分页查询
        IPage<GraduationDestination> resultPage = graduationDestinationMapper.selectPage(pagination, queryWrapper);

        // 获取查询结果
        List<GraduationDestination> graduationDestinationList = resultPage.getRecords();

        // 转换为 GraduationDestinationVo 对象
        List<GraduationDestinationVo> voList = graduationDestinationList.stream().map(destination -> {
            GraduationDestinationVo vo = new GraduationDestinationVo();
            // 查询用户信息
            User user = userMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getAccount, destination.getAccount()));
            // 查询学生信息
            Student student = studentMapper.selectOne(new LambdaQueryWrapper<Student>().eq(Student::getAccount, destination.getAccount()));

            vo.setAccount(destination.getAccount());
            vo.setUserName(user != null ? user.getUserName() : null);
            vo.setMajor(student != null ? student.getMajor() : null);
            vo.setCollege(student != null ? student.getCollege() : null);
            vo.setCompanyName(destination.getCompanyName());
            vo.setStation(destination.getStation());
            vo.setAddress(destination.getAddress());
            vo.setWage(destination.getWage());

            return vo;
        }).collect(Collectors.toList());

        // 返回分页结果
        PageVo pageVo = new PageVo(voList, resultPage.getTotal());
        return ResponseResult.okResult(pageVo);
    }


//    @Override
//    public ResponseResult graduationDestinationListAll() {
//        List<GraduationDestination> list = graduationDestinationMapper.selectList(new LambdaQueryWrapper<>());
//
//        List<GraduationDestinationVo> voList = list.stream().map(destination -> {
//            GraduationDestinationVo vo = new GraduationDestinationVo();
//            // 查询用户信息
//            User user = userMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getAccount, destination.getAccount()));
//            // 查询学生信息
//            Student student = studentMapper.selectOne(new LambdaQueryWrapper<Student>().eq(Student::getAccount, destination.getAccount()));
//            vo.setAccount(destination.getAccount());
//            vo.setUserName(user.getUserName());
//            vo.setMajor(student.getMajor());
//            vo.setCollege(student.getCollege());
//            vo.setCompanyName(destination.getCompanyName());
//            vo.setStation(destination.getStation());
//            vo.setAddress(destination.getAddress());
//            vo.setWage(destination.getWage());
//            // 其他信息可以根据需要设置到 vo 对象中
//            return vo;
//        }).collect(Collectors.toList());
//
//        return ResponseResult.okResult(voList);
//    }
@Override
public ResponseResult graduationDestinationListAll() {
    // 获取当前登录教师账号
    String teacherAccount = "100513gzu";

    // 查询教师所管理的学生账号
    LambdaQueryWrapper<TeacherStudent> tsWrapper = new LambdaQueryWrapper<>();
    tsWrapper.eq(TeacherStudent::getTeacherAccount, teacherAccount);
    List<String> studentAccounts = teacherStudentMapper.selectList(tsWrapper)
            .stream()
            .map(TeacherStudent::getStudentAccount)
            .collect(Collectors.toList());

    // 如果没有管理的学生，直接返回空分页
    if (studentAccounts.isEmpty()) {
        PageVo emptyPage = new PageVo(Collections.emptyList(), 0L);
        return ResponseResult.okResult(emptyPage);
    }

    // 查询毕业去向信息
    LambdaQueryWrapper<GraduationDestination> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.in(GraduationDestination::getAccount, studentAccounts);

    List<GraduationDestination> list = graduationDestinationMapper.selectList(queryWrapper);

    // 转换为 GraduationDestinationVo 对象
    List<GraduationDestinationVo> voList = list.stream().map(destination -> {
        GraduationDestinationVo vo = new GraduationDestinationVo();
        // 查询用户信息
        User user = userMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getAccount, destination.getAccount()));
        // 查询学生信息
        Student student = studentMapper.selectOne(new LambdaQueryWrapper<Student>().eq(Student::getAccount, destination.getAccount()));
        vo.setAccount(destination.getAccount());
        vo.setUserName(user != null ? user.getUserName() : null);
        vo.setMajor(student != null ? student.getMajor() : null);
        vo.setCollege(student != null ? student.getCollege() : null);
        vo.setCompanyName(destination.getCompanyName());
        vo.setStation(destination.getStation());
        vo.setAddress(destination.getAddress());
        vo.setWage(destination.getWage());
        // 其他信息可以根据需要设置到 vo 对象中
        return vo;
    }).collect(Collectors.toList());

    return ResponseResult.okResult(voList);
}

}



